package com.gougou.front.web.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.gougou.common.entity.ItemResult;
import com.gougou.httpclient.service.HttpAPIService;
import com.gougou.redis.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
//@RequestMapping("/index")
public class IndexController {
    @Value("${httpRootUrl}")
    String HttpUrl;

    @Autowired
    private HttpAPIService httpAPIService;
    @Autowired
    private RedisService redisService;

    /************************
     *
     * 得到大广告页地址
     *
     * 添加reids缓存优化
     *
     * @return
     * **********************
     */
    @RequestMapping("/")
    public ModelAndView getindex(){
        ModelAndView modelAndView = new ModelAndView("/views/index");
        String ad1jsonStr=null;
        String ad2jsonStr=null;
        String ad3jsonStr=null;
        //先从redi缓存中查询有没有值
        String rAd1JsonStr= redisService.get("ad1jsonStr");
        String rAd2JsonStr=redisService.get("ad2jsonStr");
        String rAd3JsonStr=redisService.get("ad3jsonStr");

        if (rAd1JsonStr!=null&&rAd2JsonStr!=null&&rAd3JsonStr!=null){
            modelAndView.addObject("indexAD1",rAd1JsonStr);
            modelAndView.addObject("indexAD2",rAd2JsonStr);
            modelAndView.addObject("indexAD3",rAd3JsonStr);
            return modelAndView;
        }

        //redis中没有查到就去httpclien拿
        try {
            ad1jsonStr = httpAPIService.doGet(HttpUrl+"/HttpAPI/queryIndexAD1");
            ad2jsonStr = httpAPIService.doGet(HttpUrl+"/HttpAPI/queryIndexAD2");
            ad3jsonStr = httpAPIService.doGet(HttpUrl+"/HttpAPI/queryIndexAD3");


        } catch (Exception e) {
            e.printStackTrace();
        }

        modelAndView.addObject("indexAD1",ad1jsonStr);
        modelAndView.addObject("indexAD2",ad2jsonStr);
        modelAndView.addObject("indexAD3",ad3jsonStr);
        return modelAndView;
    }


    /*************************
     *
     * 根据id封装商品信息，并跳转到商品详情页
     *
     * @param id
     * @return
     * @throws Exception
     * ************************
     */
    @RequestMapping("/item/{id}")
    public ModelAndView getRedis(@PathVariable long id) throws Exception {
        ModelAndView modelAndView = new ModelAndView("/views/item");

        //从redis中拿数据
        String redisJsonStr= redisService.get("item"+id);
        if (redisJsonStr!=null){
            ItemResult itemResult = JSON.parseObject(redisJsonStr, new TypeReference<ItemResult>() {});
            modelAndView.addObject(itemResult);
            return modelAndView;
        }

        //未能从缓存中找到时：从后台api拿数据，然后存储redis
        String itemJson=httpAPIService.doGet(HttpUrl+"/HttpAPI/item/"+id);

        ItemResult itemResult = JSON.parseObject(itemJson, new TypeReference<ItemResult>() {});
        modelAndView.addObject(itemResult);

        return modelAndView;
    }








}
